package com.liuyu.lottery.controller;

import java.util.List;

import com.liuyu.common.core.core.domain.R;
import com.liuyu.common.excel.utils.ExcelUtil;
import com.liuyu.common.log.annotation.Log;
import com.liuyu.common.log.enums.BusinessType;
import com.liuyu.common.orm.core.page.TableDataInfo;
import com.liuyu.common.web.annotation.RepeatSubmit;
import com.liuyu.common.web.core.BaseController;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import jakarta.annotation.Resource;
import com.liuyu.lottery.domain.vo.MailVo;
import com.liuyu.lottery.domain.bo.MailBo;
import com.liuyu.lottery.service.IMailService;


/**
 * MailControllerController
 *
 * @author liuyu
 * 2024-03-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mail")
public class MailController extends BaseController
{
    @Resource
    private IMailService mailService;

    /**
     * 查询MailController列表
     */
    @SaCheckPermission("lottery:IMailService:list")
    @GetMapping("/list")
    public TableDataInfo<MailVo> list(MailBo mailBo)
    {
        return mailService.selectPage(mailBo);
    }

    /**
     * 导出MailController列表
     */
    @SaCheckPermission("lottery:IMailService:export")
    @Log(title = "MailController", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MailBo mailBo)
    {
        List<MailVo> list = mailService.selectList(mailBo);
        ExcelUtil.exportExcel(list, "MailController", MailVo.class, response);
    }

    /**
     * 获取MailController详细信息
     */
    @SaCheckPermission("lottery:IMailService:query")
    @GetMapping(value = "/{id}")
    public R<MailVo> getInfo(@PathVariable Integer id)
    {
        return R.ok(mailService.selectById(id));
    }

    /**
     * 新增MailController
     */
    @SaCheckPermission("lottery:IMailService:add")
    @Log(title = "MailController", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public R<Void> add(@Validated @RequestBody MailBo mailBo)
    {
        boolean inserted = mailService.insert(mailBo);
        if (!inserted) {
            return R.fail("新增MailController记录失败！");
        }
        return R.ok();
    }

    /**
     * 修改MailController
     */
    @SaCheckPermission("lottery:IMailService:edit")
    @Log(title = "MailController", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public R<Void> edit(@Validated @RequestBody MailBo mailBo)
    {
        Boolean updated = mailService.update(mailBo);
        if (!updated) {
            return R.fail("修改MailController记录失败!");
        }
        return R.ok();
    }

    /**
     * 删除MailController
     */
    @SaCheckPermission("lottery:IMailService:remove")
    @Log(title = "MailController", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable Integer[] ids)
    {
        boolean deleted = mailService.deleteByIds(ids);
        if (!deleted) {
            return R.fail("删除MailController记录失败!");
        }
        return R.ok();
    }

    @GetMapping("/search")
    public R<List<MailBo>> search(@RequestParam(required = false) String fromUser, @RequestParam(required = false) String toUser) {
        return mailService.search(fromUser,toUser);
    }
}
